package org.codingq92.pritice.socket;

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author: CodingQ92
 * @date: 2025/6/18 00:10
 */
public class TcpClient {
    public static void main(String[] args) {
        new Thread(TcpClient::client, "C1").start();
        new Thread(TcpClient::client, "C2").start();
        new Thread(TcpClient::client, "C3").start();
    }

    private static void client() {
        try (Socket socket = new Socket("127.0.0.1", 9000); // 连接服务端
             OutputStream outputStream = socket.getOutputStream(); // 获取输出流
             DataOutputStream dos = new DataOutputStream(outputStream)) { // 获取数据输出流
            for (int i = 0; i < 10; i++) {
                dos.writeUTF(Thread.currentThread().getName() + ": hello_" + i); // 发送数据 一定要和服务端的读取数据方法一致
                System.out.println("发送数据: hello server_" + i);
                Thread.sleep(1000);
            }
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
